document.write('<script language=javascript src="js/jquery-3.5.1.min.js"></script>');
//document.write('<script language=javascript src="js/jquery-3.6.0.min.js"></script>');
document.write('<link rel="stylesheet" href="css/register.css">');
/*
var xhr;
function checkUsername(){
    var username = document.getElementById("username").value;//拿到用户输入的值
    console.log(username);
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = fun1;//状态改变时调用函数
    //向服务器发送请求
    xhr.open("GET","usernameExit?username="+username,true);
    xhr.send(null);
}
//回调函数
function fun1(){
    if(xhr.readyState===4){
        if(xhr.status===200){
            //成功
            var tips=document.getElementById("usernameTips");
            var responseInfo = xhr.responseText;
            console.log(responseInfo);
            if(responseInfo==="Exist"){
                //用户名不可用
                tips.className="invalid";
                tips.innerText="Invalid";
            }else if(responseInfo==="Not Exist"){
                //用户名可用
                tips.className="available";
                tips.innerText="Available";
            }
        }
    }
}
*/
$(function (){
    $('#username').on('blur',function (){
        $.ajax({
            type    : "GET",
            url     : "usernameExist?username="+this.value,
            success : function (data){
                if(data.code===2){
                    $('#usernameTips').attr("class","invalid").text("输入不可为空");//样式
                }else if(data.code===0){
                    $('#usernameTips').attr("class","invalid").text("用户名不可用");//样式
                    //$('#usernameTips').text = "Invalid";//内容
                }else if(data.code===1){
                    $('#usernameTips').attr("class","available").text("用户名可用");
                }
            }
        });
    });
});
$(function (){
    $('#password').on('blur',function (){
        if(this.value.length===0){
            $('#passwordTips').attr("class","invalid").text("输入不可为空");
        }else{
            $('#passwordTips').attr("class","available").text("格式正确");
        }
    });
});
$(function (){
    $('#repeatedPassword').on('blur',function (){
        if(this.value.length===0){
            //重复密码为空
            $('#repeatedPasswordTips').attr("class","invalid").text("输入不可为空");
        }else if(this.value==document.getElementById("password").value){
            //重复密码输入正确
            $('#repeatedPasswordTips').attr("class","available").text("输入密码一致");
        }else{
            $('#repeatedPasswordTips').attr("class","invalid").text("输入密码不一致");
        }
    });
});
$(function (){
    $('#vCode').on('blur',function (){
        $.ajax({
            type    : "GET",
            url     : "checkCode?vCode="+this.value,
            success : function (data){
                if(data.code===3){
                    //验证码框未输入
                    $('#vCodeTips').attr("class","invalid").text("输入不可为空");
                }else if(data.code===4){
                    $('#vCodeTips').attr("class","available").text("验证码正确");
                }else if(data.code===5){
                    $('#vCodeTips').attr("class","invalid").text("验证码错误");
                }
            }
        });
    });
});

$(function (){
    $('#newAccount').on('click',function (){
        var result;
        var result1;
        var result2;
        $.ajax({
            type    : "GET",
            url     : "usernameExist?username="+document.getElementById("username").value,
            success : function (data){
                if(data.code===1){
                    result=1;
                    if(document.getElementById("password").value.length===0){
                        $('#newAccountTips').attr("class","invalid").text("请输入正确信息");
                    }else if(document.getElementById("repeatedPassword").value==document.getElementById("password").value){
                        //重复密码输入正确
                        result1=1;
                    }else{
                        $('#newAccountTips').attr("class","invalid").text("请输入正确信息");
                    }
                    $.ajax({
                        type    : "GET",
                        url     : "checkCode?vCode="+document.getElementById("vCode").value,
                        success : function (data){

                            if(data.code===4){
                                result2=1;
                            }
                            if(result==1&&result1==1&&result2==1){
                                var formElement = document.forms[1];
                                formElement.submit();
                            }else{
                                $('#newAccountTips').attr("class","invalid").text("请输入正确信息");
                            }
                        }
                    });
                }else{
                    $('#newAccountTips').attr("class","invalid").text("请输入正确信息");
                }
            }
        });
    });
});

